//
//  LoginBrain.m
//  tradersOnMap
//
//  Created by bilmuh bilmuh on 09.03.2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LoginBrain.h"

@implementation LoginBrain

@synthesize userId, email, password;

- (void)login
{
	
	//send http request to login
	NSString *urlstr = [[NSString alloc] initWithFormat:@"http://sorubank.ege.edu.tr/~b05067683/tez/login.php?email=%@&password=%@",email,password];
	NSURL *url = [[NSURL alloc] initWithString:urlstr];
	NSURLRequest *urlReq = [NSURLRequest requestWithURL:url];
	[[NSURLConnection alloc] initWithRequest:urlReq delegate:self];
	
	//receiving data and parsing
	data = [NSData dataWithContentsOfURL:url];
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
	[parser setDelegate:self];
	[parser parse];
	[parser release];
}

- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
	attributes:(NSDictionary *)attributeDict
{
	if ([elementName isEqual:@"account"]) {
		defaults = [[NSUserDefaults alloc] init];
		return;
	}
	textInProgress = [[NSString alloc] init];
}

- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{
	if ([elementName isEqual:@"id"]) {
		[defaults setObject:textInProgress forKey:@"userID"];
		self.userId = textInProgress;
	}else if ([elementName isEqual:@"name"]) {
		[defaults setObject:textInProgress forKey:@"firstName"];
	}else if ([elementName isEqual:@"lastName"]) {
		[defaults setObject:textInProgress forKey:@"lastName"];
	}else if ([elementName isEqual:@"phoneNumber"]) {
		[defaults setObject:textInProgress forKey:@"phoneNumber"];
	}else if ([elementName isEqual:@"imageURL"]) {
		[defaults setObject:textInProgress forKey:@"imageURL"];
	}else if ([elementName isEqual:@"adress"]) {
		[defaults setObject:textInProgress forKey:@"adress"];
	}else if ([elementName isEqual:@"neighborhood"]) {
		[defaults setObject:textInProgress forKey:@"neighborhood"];
	}else if ([elementName isEqual:@"borough"]) {
		[defaults setObject:textInProgress forKey:@"borough"];
	}else if ([elementName isEqual:@"city"]) {
		[defaults setObject:textInProgress forKey:@"city"];
	}else if ([elementName isEqual:@"account"]) {
		[defaults release];
		return;
	}
	[textInProgress release];
}

-(void)parser:(NSXMLParser *)parser
   foundCDATA:(NSData *)CDATABlock
{
	textInProgress = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}

@end
